package com.anyext.storage;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Page implements Serializable {

	private int	dataCount	= 0;	// 总条数
	private int	pageCount	= 1;	// 总页数
	private int	pageNumber	= 1;	// 当前页数
	private int	pageSize	= 20;	// 每页条数

	public Page(int pageSize) {
		this.pageSize = Math.max(1, pageSize);
	}

	public boolean hasData() {
		return this.pageCount > 0;
	}

	public int getDataCount() {
		return this.dataCount;
	}

	public void setDataCount(int dataCount) {
		this.dataCount = Math.max(0, dataCount);
		this.computePageCount();
	}

	public int getPageNumber() {
		return this.pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = Math.max(1, Math.min(this.pageCount, pageNumber));
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = Math.max(1, pageSize);
		this.computePageCount();
	}

	public int getFirstResult() {// 从1开始
		return this.pageSize * (this.pageNumber - 1) + 1;
	}

	public int getMaxResult() {
		return this.pageSize;
	}

	public int getPrevPageNumber() {
		return Math.max(1, this.pageNumber - 1);
	}

	public int getNextPageNumber() {
		return Math.min(this.pageCount, this.pageNumber + 1);
	}

	public int getFirstRowNumber() {
		return Math.min(this.dataCount, getFirstResult());
	}

	public int getLastRowNumber() {
		return Math.min(this.dataCount, this.getFirstRowNumber() + this.pageSize - 1);
	}

	public boolean disabledPrevPage() {
		return this.pageCount <= 1 || this.pageNumber <= 1;
	}

	public boolean disabledNextPage() {
		return this.pageCount <= 1 || this.pageNumber >= this.pageCount;
	}

	public boolean disabledTurnTo() {
		return this.pageCount <= 1;
	}

	public void computePageCount() {
		this.pageCount = Math.max(1, this.dataCount / this.pageSize + Math.min(1, this.dataCount % this.pageSize));
		this.pageNumber = Math.min(this.pageCount, this.pageNumber);
	}

	public int getPageCount() {
		return this.pageCount;
	}

	public void reset(int pageSize) {
		this.dataCount = 0;
		this.pageCount = 1;
		this.pageNumber = 1;
		this.pageSize = Math.max(1, pageSize);
	}

	public String toString() {
		return "向前[" + !this.disabledPrevPage() + "]  前页[" + this.getPrevPageNumber() + "] 当前页[" + this.getPageNumber() + "] 数据[" + this.getFirstRowNumber() + "-" + this.getLastRowNumber() + "] 下页[" + this.getNextPageNumber() + "] 向后[" + !this.disabledNextPage() + "] 跳转[" + !this.disabledTurnTo() + "] 总页[" + this.getPageCount() + "] 总数[" + this.getDataCount() + "] 总数据[" + this.getFirstResult() + "-" + (this.getFirstResult() + this.getMaxResult()) + "] 每页[" + this.getPageSize() + "]";
	}

	public static void main(String[] args) {
		Page page = new Page(15);
		System.out.println(page);
		page.setDataCount(16);
		System.out.println(page);
		page.setPageSize(20);
		System.out.println(page);
		page.setPageNumber(2);
		System.out.println(page);
		page.setDataCount(25);
		System.out.println(page);
		page.setPageNumber(2);
		System.out.println(page);
		page.setDataCount(100);
		System.out.println(page);
		page.setPageNumber(5);
		System.out.println(page);
		page.setDataCount(0);
		System.out.println(page);
		page.setDataCount(1);
		System.out.println(page);
		page.setDataCount(125);
		page.setPageSize(25);
		System.out.println(page);
		page.setPageNumber(4);
		System.out.println(page);
	}
}
